package com.qf.fmcommodity.user.controller;

import com.qf.fmcommodity.user.entity.ShoppingCart;
import com.qf.fmcommodity.user.service.IShoppingCartService;
import com.qf.fmcommodity.user.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车  前端控制器
 * </p>
 *
 * @author 苇名一心
 * @since 2023-02-13
 */
@CrossOrigin
@RestController
@RequestMapping("/shopcart")
public class ShoppingCartController {


    @Autowired
    IShoppingCartService shoppingCartService;

    @RequestMapping("/listbycids")
    public LoginVo listbycids(String[] cids){

    List<ShoppingCart> data = shoppingCartService.listbycids(cids);

    return LoginVo.ok(data);

    }

    @PutMapping("/update/{cid}/{sum}")
    public LoginVo update(@PathVariable("cid") Integer cid, @PathVariable("sum") Integer sum){

        shoppingCartService.update(cid,sum);

        return LoginVo.ok();

    }

    @GetMapping("/list")
    public LoginVo list(Integer userId){

        List<ShoppingCart> data = shoppingCartService.listShopcart(userId);

        return LoginVo.ok(data);
    }

    @PostMapping("/add")
    public LoginVo add(@RequestBody ShoppingCart shoppingCart){
        //添加购物车
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());

        shoppingCart.setCartTime(format);

        shoppingCartService.save(shoppingCart);

        return LoginVo.ok();

    }

}
